package com.st.controller;

import com.api.login.ILoginClient;
import com.st.entity.Result;
import com.st.entity.User;
import com.st.entity.UserDto;
import com.st.service.IRegisterService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import util.ImageCodeUtil;

import javax.imageio.ImageIO;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.image.BufferedImage;
import java.io.IOException;


@Controller
public class RegisterController {
    @Autowired
    private IRegisterService iRegisterService;
    //获取图形验证码
    @RequestMapping("/pigCode")
    public void pigCode(HttpServletResponse response) throws IOException {
        Result result = iRegisterService.pigCode();
        if (result.isSuccess()){
            BufferedImage image = (BufferedImage) result.getData();
            ServletOutputStream outputStream = response.getOutputStream();
            ImageIO.write(image, "jpg", outputStream);

        }

    }
    //发送手机验证码
    @RequestMapping("/sendMsgCode")
    public Result sendMsgCode(String phone){
        Result result = iRegisterService.sendMsgCode(phone);
        return result;
    }
    //用户注册
    @RequestMapping("/register")
    @ResponseBody
    public Result register(@RequestBody UserDto userDto){
        return iRegisterService.register(userDto);
    }
}
